package com.yanqu.road.server.cmd.newmonopolymkt.office;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.newmonopolymarket.NewMonopolyMarketOfficeUserSeatLogData;
import com.yanqu.road.entity.log.newmonopolymarket.NewMonopolyMarketUserPhysicalChangeLogData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_OFFICE_ACTION_FROM_CROSS, desc = "大宋榷署撤销/抢夺 位置 跨服返回区服 协议")
public class NewMonopolyMarketOfficeActionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg resp = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg.parseFrom(packet.getBytes());
        Property property = null;
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        if(resp.getType() == 0){
            // 解除预扣除
            if(resp.hasConsume()){
                property = PropertyHelper.parseStringToProperty(resp.getConsume());
                player.getModule(PreReduceModule.class).restorePreReduce(property);
            }
        }
        if(resp.getStcMsg().getRet() == 0){
            if(resp.getType() == 0){
                // 入座
                if(property != null){
                    player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketOfficeConsume);
                }
                marketModule.userData.setOfficeRobTimes(marketModule.userData.getOfficeRobTimes() + 1);
                if(resp.getStcMsg().getIsSuccess()){
                    // 成功抢夺
                    NewMonopolyMarketMgr.refreshUser(player,true,false);

                    AutoLogMgr.add(new NewMonopolyMarketOfficeUserSeatLogData(
                            player.getUserId(), marketModule.userData.getWeekId(), 0, marketModule.userData.getOfficeCurPos(), marketModule.userData.getOfficeLevel(),
                            marketModule.userData.getOfficeNo(),3,
                            marketModule.userData.getGuanYin(), marketModule.userData.getOfficeSeatTime()));
                    NewMonopolyMarketMgr.sitOffice(marketModule, resp.getStcMsg().getSeatInfo(),resp.getSeatLevel(),resp.getNo());
                }
                //抢夺成就
                player.notifyListener(eGamePlayerEventType.MonopolyMarketJoinOfficeTimes.getValue(), 1);
            }else {
                // 撤回
                int oldValue = marketModule.userData.getGuanYin();

                NewMonopolyMarketMgr.refreshUser(player,true,false);

                AutoLogMgr.add(new NewMonopolyMarketOfficeUserSeatLogData(player.getUserId(), marketModule.userData.getWeekId(), resp.getResGroupId(), marketModule.userData.getOfficeCurPos(),
                        marketModule.userData.getOfficeLevel(),marketModule.userData.getOfficeNo(),2, marketModule.userData.getGuanYin(),marketModule.userData.getOfficeSeatTime()));

                NewMonopolyMarketMgr.resetOfficeSeat(marketModule);

                AutoLogMgr.add(new NewMonopolyMarketUserPhysicalChangeLogData(player.getUserId(), marketModule.userData.getWeekId(),
                        marketModule.userData.getOfficeCurPos(),marketModule.userData.getOfficeLevel(), marketModule.userData.getOfficeNo(),2, oldValue, marketModule.userData.getGuanYin()));
            }
            marketModule.synRoleData();
        }
        player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_OFFICE_ACTION, resp.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
